(function($) {
	$(function(event) {
		$('#frm-sup-fn').focus();

		$('#frm-signup').submit(function() {
			$('div.panel-error').remove();
		});

		if ($('#selector-city').val() == -1) {
			$('#show-no-city').show();
			$('#no-city').rules('add', {required: true});
		} else {
			$('#show-no-city').hide();
		}

		$('#selector-city').click(function() {
			var val = $('#selector-city').val();
			if(val == -1) {
				$('#show-no-city').show();
				$('#no-city').rules('add', {required: true});
			} else {
				$('#show-no-city').hide();
				$('#no-city').rules('add', {required: false});
			}
		});

		$('#frm-sup-cnt').change(function() {
			$('#show-no-city').hide();
			$('input#no-city').val('');
			var s = $(this);
			var t = $('#selector-city'), p = {id: s.val()};
	        t.html('<option value="">' + 'LBL_LOADING'.translate() + '</option>');
	        t.attr('disabled', 'disabled');
	        $.get('/a_ajax/location.getCitiesByCountryCode', p, function (data) {
	            t.attr('disabled', '');
	            var h = '';
	            h += '<option value="" selected="selected" class="first">' + s.attr('deftext') + '</option>';
	            t.html(h + data);
	        });
		});

//		function validateForm() {
			var
				mesReq = 'REQUIRED_FIELD'.translate(),
				mesPas = 'PASSWORDS_DONT_MATCH'.translate(),
				mesEmail = 'INCORRECT_EMAIL'.translate(),
				mesDay = 'CHOOSE_DAY'.translate(),
				mesMonth = 'CHOOSE_MONTH'.translate(),
				mesYear = 'CHOOSE_YEAR'.translate()
			;
			$.validator.messages.required = mesReq;
			var validateParams = {
				messages: {
					email: {
						email: mesEmail
					},
					retPassword: {
						equalTo: mesPas
					},
					day: {
						required: mesDay
					},
					month: {
						required: mesMonth
					},
					year: {
						required: mesYear
					}
				},
				errorClass: 'invalid',
				rules: {
					retPassword: {
						equalTo: '#frm-sup-psw1'
					}
				},
				errorPlacement: function(error, element) {
					if (element.attr("id") == "frm-sup-agreement")
						error.appendTo(".chckPlaceholder");
					else if (element.attr("name") == "day" || element.attr("name") == "month" || element.attr("name") == "year")
						error.appendTo(".datePlaceholder");
					else
						error.insertAfter(element);
				}
			};
			var validator = $('#frm-signup').validate(validateParams);
			
//		}
		
		var fn_changeRegType = function(sRegType)
		{
			var ag = $('dl.ag');
			var corporation = $('dl.corporation');
			var agent = $('dl.agent');
			var agentBusiness = $('dl.agent-business');
			var birthdate = $('dl.birthdate');
			var agreement = $('div#agreement');
			var promo = $('dl.promo');

			ag.hide();
			corporation.hide();
			agent.hide();
			agentBusiness.hide();
			birthdate.hide();
			agreement.hide();
			promo.hide();

			switch(sRegType) {
				case 'agency':
					ag.show();
					promo.show();
				break;
				case 'operator':
					ag.show();
					promo.show();
				break;
				case 'user':
					birthdate.show();
					agreement.show();
					promo.show();
				break;
				case 'corporation':
					corporation.show();
					promo.show();
				break;
				case 'agent':
					agent.show();
				break;
				case 'agent-business':
					agentBusiness.show();
				break;
			}
		};

		var initRules = function(regType) {
			$('#frm-signup label.invalid').remove();
			$('#frm-signup .invalid').removeClass('invalid');

			$('#frm-name-ag').rules('add', {required: false});
			$('#frm-name-ag-p').rules('add', {required: false});
			$('#frm-post-addr').rules('add', {required: false});
			$('#frm-telphone').rules('add', {required: false});
			$('#frm-bd').rules('add', {required: false});
			$('.birthday-month').rules('add', {required: false});
			$('.birthday-year').rules('add', {required: false});
			$('#frm-sup-agreement').rules('add', {required: false});
			$('#frm-name-corporation').rules('add', {required: false});
			$('#frm-post-addr-corporation').rules('add', {required: false});
			$('#frm-telphone-corporation').rules('add', {required: false});
			$('#frm-sup-addr').rules('add', {required: false});
			$('#frm-sup-addr-resid').rules('add', {required: false});
			$('#user-frm-sup-pne').rules('add', {required: false});
			$('#frm-sup-bsns-bus-cny').rules('add', {required: false});
			$('#frm-sup-bsns-fys-cny').rules('add', {required: false});
			$('#frm-sup-bsns-addr').rules('add', {required: false});
			$('#frm-sup-bsns-addr-resid').rules('add', {required: false});
			$('#frm-sup-bsns-pne').rules('add', {required: false});

			switch (regType) {
				case 'agency':
					$('#frm-name-ag').rules('add', {required: true});
					$('#frm-name-ag-p').rules('add', {required: true});
					$('#frm-post-addr').rules('add', {required: true});
					$('#frm-telphone').rules('add', {required: true});
					break;
				case 'operator':
					$('#frm-name-ag').rules('add', {required: true});
					$('#frm-name-ag-p').rules('add', {required: true});
					$('#frm-post-addr').rules('add', {required: true});
					$('#frm-telphone').rules('add', {required: true});
					break;
				case 'user':
					$('#frm-bd').rules('add', {required: true});
					$('.birthday-month').rules('add', {required: true});
					$('.birthday-year').rules('add', {required: true});
					$('#frm-sup-agreement').rules('add', {required: true});
					$('#user-frm-sup-pne').rules('add', {required: true});
					break;
				case 'corporation':
					$('#frm-name-corporation').rules('add', {required: true});
					$('#frm-post-addr-corporation').rules('add', {required: true});
					$('#frm-telphone-corporation').rules('add', {required: true});
					break;
				case 'agent':
					$('#frm-sup-addr').rules('add', {required: true});
					$('#frm-sup-addr-resid').rules('add', {required: true});
					break;
				case 'agent-business':
					$('#frm-sup-bsns-bus-cny').rules('add', {required: true});
					$('#frm-sup-bsns-fys-cny').rules('add', {required: true});
					$('#frm-sup-bsns-addr').rules('add', {required: true});
					$('#frm-sup-bsns-addr-resid').rules('add', {required: true});
					$('#frm-sup-bsns-pne').rules('add', {required: true});
					break;
			}
		};

		var srt = $('select#selectorRegType');
		
		var requestUserType = location.href.match(/#(.+)$/);
		if (requestUserType) {
			srt.val(requestUserType[1]);
		}
		
		fn_changeRegType(srt.val());
		
		srt.change(function() {fn_changeRegType(this.value);});

		if (srt.val() != '' && srt.val() != undefined) {
			initRules(srt.val());
		}

		$('#selectorRegType').change(function() {
			initRules($(this).val());
		});

		var radioAgentPersonal = $('#agent-personal');
		var radioAgentBusiness = $('#agent-business');

		if (radioAgentPersonal.is(':checked')) {
			showAgentPersonal();
			initPersonalRules();
		} else if (radioAgentBusiness.is(':checked')) {
			showAgentBusiness();
			initBusinessRules();
		}



		radioAgentPersonal.click(function() {
			showAgentPersonal();
			initPersonalRules();
		});

		radioAgentBusiness.click(function() {
			showAgentBusiness();
			initBusinessRules();
		});

		function initBusinessRules() {
			$('#frm-sup-prnl-addr').rules('add', {required: false});
			$('#frm-sup-prnl-addr-resid').rules('add', {required: false});
			$('#frm-sup-bsns-bus-cny').rules('add', {required: true});
			$('#frm-sup-bsns-fys-cny').rules('add', {required: true});
			$('#frm-sup-bsns-addr').rules('add', {required: true});
			$('#frm-sup-bsns-addr-resid').rules('add', {required: true});
			$('#frm-sup-bsns-pne').rules('add', {required: true});
		}

		function initPersonalRules() {
			$('#frm-sup-prnl-addr').rules('add', {required: true});
			$('#frm-sup-prnl-addr-resid').rules('add', {required: true});
			$('#frm-sup-bsns-bus-cny').rules('add', {required: false});
			$('#frm-sup-bsns-fys-cny').rules('add', {required: false});
			$('#frm-sup-bsns-addr').rules('add', {required: false});
			$('#frm-sup-bsns-addr-resid').rules('add', {required: false});
			$('#frm-sup-bsns-pne').rules('add', {required: false});
		}

		function showAgentPersonal() {
			$('dl.personal').show();
			$('dl.business').hide();
		}

		function showAgentBusiness() {
			$('dl.personal').hide();
			$('dl.business').show();
		}

	});
})(jQuery);
